package controllers

import (
	"gdshop-font-api-go/app/request/AddressReq"
	"gdshop-font-api-go/app/request/OrderReq"
	"gdshop-font-api-go/app/service/AddressService"
	"gdshop-font-api-go/app/service/OrderService"
	"gdshop-font-api-go/library/response"
	"gdshop-font-api-go/library/tools/token"
	"github.com/gogf/gf/net/ghttp"
)

type OrderController struct {
	*BaseController
}

func (c *OrderController) Init(r *ghttp.Request) *response.JsonResponse {
	memberId = token.GetLoginMemberId(r)
	if memberId < 1 {
		return response.FailByRequestMessage(r, "获取会员ID失败")
	}
	return response.SuccessByRequest(r)
}

// 订单列表
func (c *OrderController) List(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.List
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.List(r.GetCtx(), parames)
}

// 订单详情
func (c *OrderController) Details(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.Details
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Details(r.GetCtx(), parames)
}

// 预下单数据获取
func (c *OrderController) Create(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.Create
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Create(r.GetCtx(), parames)
}

// 提交订单
func (c *OrderController) Submit(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.SubmitGroup
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Submit(r.GetCtx(), parames)
}
func (c *OrderController) CheckSubmitOk(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.CheckSubmitOk
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	return OrderService.CheckSubmitOk(r.GetCtx(), parames)
}
func (c *OrderController) Del(r *ghttp.Request) *response.JsonResponse {
	var parames *AddressReq.Del
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return AddressService.Del(r.GetCtx(), parames)
}

func (c *OrderController) Pay(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.Pay
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Pay(r.GetCtx(), parames)
}

func (c *OrderController) SubmitPay(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.SubmitPay
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.SubmitPay(r.GetCtx(), parames)
}

// UseCoupon 下单前，显示可使用的优惠券
func (c *OrderController) UseCoupon(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.UseCoupon
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.UseCoupon(r.GetCtx(), parames)
}

func (c *OrderController) OrderSubAftersale(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.OrderSubAftersale
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.OrderSubAftersale(r.GetCtx(), parames)
}

func (c *OrderController) SaveOrderSubAftersale(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.SaveOrderSubAftersale
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.SaveOrderSubAftersale(r.GetCtx(), parames)
}

func (c *OrderController) Close(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.Close
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Close(r.GetCtx(), parames)
}
func (c *OrderController) Confirm(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.Confirm
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Confirm(r.GetCtx(), parames)
}

func (c *OrderController) Logistics(r *ghttp.Request) *response.JsonResponse {
	var parames *OrderReq.Logistics
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	parames.MemberId = memberId
	return OrderService.Logistics(r.GetCtx(), parames)
}
